from sqlalchemy import create_engine,inspect

from app.config.db import db_config
import importlib
# 根据Model创建DB数据表

if __name__ == '__main__':
    # 创建db连接
    engine = create_engine(db_config.get_url(), echo=True)
    model_module=importlib.import_module("app.model.file")
    create_model=getattr(model_module,"File")
    create_indexes=getattr(model_module,"indexes")
    # print('Base.metadata',Base.metadata.create_all)

    # 删除表
    # create_model.metadata.drop_all(engine)

    # 创建db表
    
    # create_model.metadata.create_all(engine)
    
    inspector = inspect(engine)
    current_indexes = [i['name'] for i in inspector.get_indexes(create_model.__tablename__)]
    for item in create_indexes:
        print(current_indexes,item.name)
        if item.name in current_indexes:
            print(f"已有{item.name}索引===",item,'====')
        else:
            item.create(engine)
            print(f"创建{item.name}索引===",item,'====')

